<?php

!defined('IN_ROOT') && exit('Access Denied');

class admin_viewscontrol extends base {

    function admin_viewscontrol(& $get,& $post) {
        $this->base( & $get,& $post);
        $this->load('views');
        $this->load('setting');
        $this->load('area');
        $this->load('sysmap');
    }

    function ondefault($msg='') {
        $msg && $message=$msg;
        @$page = max(1, intval($this->get[2]));
        $pagesize=$this->setting['list_default'];
        $startindex = ($page - 1) * $pagesize;
        $viewslist = $_ENV['views']->get_list($startindex,$pagesize);
        $viewsnum=$this->db->fetch_total('views');
        $departstr=page($viewsnum, $pagesize, $page,"admin_views/default");
        $area_js=$_ENV['area']->select_option_cate();
        include template('viewslist','admin');
    }
    function onsearch(){
        
        @$page = max(1, intval($this->get[4]));
        $pid = isset($this->post['pid'])?$this->post['pid']:$this->get[2];
        $viewsname =isset($this->post['viewsname'])?$this->post['viewsname']:$this->get[3];
        $pagesize= 20;
        $startindex = ($page - 1) * $pagesize;
        $rangesql = '';
        $ranges = explode("-",$range);
        $viewslist = $_ENV['views']->get_by_range_name($pid,$viewsname,$startindex,$pagesize);
        ((int)$pid>0) && $rangesql= "AND `sid`=$pid";
        $rownum=$this->db->fetch_total('views'," `name` LIKE '%$viewsname%' $rangesql");
        $departstr=page($rownum, $pagesize, $page,"admin_views/search/$pid/$viewsname");
        $area_js=$_ENV['area']->select_option_cate($pid);
        include template('viewslist','admin');
    }
    function onadd() {
        if(isset($this->post['submit'])) {
            $sid = (int)$this->post['pid'];
            tcookie('viewsid',$sid,36000);
            $name = $this->post['name'];
            $sname = $this->post['sname'];
            $keywords = $this->post['keywords'];
            $descs = $this->post['descs'];
            $adress = $this->post['adress'];
            $tel = $this->post['tel'];
            $cates = $this->post['cates'];
            if(''==$name || ''==$sname || $sid==0) {
                msg("请正确填写景点相关信息！",'index.php?admin_views/add');
            }
            $pics = '';
            if($_FILES['imgurl']['name']){
            	$imgname=strtolower($_FILES['imgurl']['name']);
            	$type = substr(strrchr($imgname,'.'),1);
            	if(!isimage($type)) {
	                msg("图片格式不支持，目前仅支持jpg、gif、png格式！",'index.php?admin_views/add');
	            }
	            $pics = 'v'.substr(md5(random(3,0).time()),0,9).'.'.$type;
	            $filepath = '/data/attach/viewsimg/'.$pics;
	            forcemkdir(MY_ROOT.'/data/attach/viewsimg');
	            move_uploaded_file($_FILES['imgurl']['tmp_name'],MY_ROOT.$filepath);
            }
            $vid = $_ENV['views']->add($sid,$name,$sname,$keywords,$descs,$adress,$tel,$pics);
            $_ENV['sysmap']->delviewscate($vid);
            $_ENV['sysmap']->inviewscate($vid,$cates);
            msg("添加成功！",'index.php?admin_views');
            
            
        }else {
        	$viewsid = (int)@tcookie('viewsid');
        	$cate_js = $_ENV['sysmap']->get_viewslist();
        	$area_js=$_ENV['area']->select_option_cate($viewsid);
            include template('addviews','admin');
        }
    }

    function onedit() {
        $gid = intval($this->get[2])?$this->get[2]:$this->post['id'];
        
        //var_dump($cate_js);
        if(isset($this->post['submit'])) {
            $sid = (int)$this->post['pid'];
            $name = $this->post['name'];
            $sname = $this->post['sname'];
            $keywords = $this->post['keywords'];
            $descs = $this->post['descs'];
            $adress = $this->post['adress'];
            $tel = $this->post['tel'];
            $imgname=strtolower($_FILES['imgurl']['name']);
            $cates = $this->post['cates'];
            if(''==$name || ''==$sname || $sid==0) {
				msg("请正确填写景点相关信息！",'index.php?admin_views/edit/'.$gid);
            }

            $type = substr(strrchr($imgname,'.'),1);
            if(!empty($_FILES['imgurl']['tmp_name'])) {
            	if((!isimage($type))){
	                msg("图片格式不支持，目前仅支持jpg、gif、png格式！",'index.php?admin_views/edit/'.$gid);
            	}
            	$pics = 'v'.substr(md5(random(3,0).time()),0,9).'.'.$type;
	            $filepath = '/data/attach/viewsimg/'.$pics;
	            forcemkdir(MY_ROOT.'/data/attach/viewsimg');
	            if(!move_uploaded_file($_FILES['imgurl']['tmp_name'],MY_ROOT.$filepath)) {
	                msg("服务器忙，请稍后再试！",'index.php?admin_views/edit/'.$gid);
	            }
                
            }
            empty($_FILES['imgurl']['tmp_name']) && $pics =$this->post['imgpath'];
            $_ENV['views']->update($sid,$name,$sname,$keywords,$descs,$adress,$tel,$pics,$gid);
            $_ENV['sysmap']->delviewscate($gid);
            $_ENV['sysmap']->inviewscate($gid,$cates);
            //$message = "修改成功!";
            msg("编辑成功!",'index.php?admin_views/edit/'.$gid);
        }

        $views= $_ENV['views']->get($gid);
        $area_js=$_ENV['area']->select_option_cate($views[sid]);
        $cate_js = $_ENV['sysmap']->get_viewslist($gid);
        include template('addviews','admin');
    }
	function onsort() {
        if(isset($this->post['submit'])) {
        	$pid = (int)$this->post['pid'];
        	$vid = (int)$this->post['vid'];
        	if(!empty($this->post['cids'])){
        		$cids = implode(",",$this->post['cids']);
        		$_ENV['map']->delcate($vid,$cids,2);
        	}
			if(!empty($this->post['cid'])){
            	$_ENV['map']->addcate($vid,$this->post['cid'],2);
			}
            msg('设置成功！',"index.php?admin_views/sort/{$vid}/$pid");
        }else {
        	$vid = intval($this->get[2]);
        	$viewdb = $_ENV['views']->get($vid);
        	$pid = intval($this->get[3]);
        	$category = $this->category[$pid];
        	if($pid && $_ENV['map']->check_has_map($vid,$pid,2)==0){
        		msg('请选择该类后再进行下级操作！',"index.php?admin_views/sort/{$vid}/$category[pid]");
        	}	
        	$categorylist = $_ENV['map']->list_by_cid_pid_site($pid,$category['pid'],$vid,2);
        	$navlist=$_ENV['map']->get_navigation($pid);
            include template('viewsort','admin');
        }
    }
    
    function onaddrange() {
        $rangelist = unserialize($this->setting['views_range']);
        if(isset($this->post['submit'])) {
            $ranges = $this->post['views_range'];
            $rangesize = count($ranges);
            $viewsrange = array();
            for($i=0;$i<$rangesize;$i++) {
                if($i>0 && $ranges[$i]!= '' && $ranges[$i]%2 == 0)
                    $viewsrange[$ranges[$i-1]]=$ranges[$i];
            }
            if((count($viewsrange)%2) != 0) {
                $message = '请正确填写价格范围，必须为整数对！';
                $type= 'errormsg';
                include template('viewsrange','admin');
                exit;
            }
            $rangelist = $viewsrange;
            $this->setting['views_range'] = serialize($viewsrange);
            $_ENV['setting']->update($this->setting);
            $message = '设置成功！';
        }
        include template('viewsrange','admin');
    }

    function onnote() {
        if(isset($this->post['submit'])) {
            $this->setting['views_note'] = $this->post['note'];
            $_ENV['setting']->update($this->setting);
            $message = '设置公告成功！';
        }
        include template('viewsnote','admin');

    }

    function onremove() {
        $message='没有选择景点！';
        if(isset($this->post['gid'])) {
            $gids = implode(",", $this->post['gid']);
            $_ENV['views']->remove_by_id($gids);
            foreach ($this->post['gid'] as $vid){
            	$_ENV['sysmap']->delviewscate($vid);
            }
            $message='景点删除成功！';
            unset($this->get);
        }
        $this->ondefault($message);
    }

    function onavailable() {
        if(isset($this->post['gid'])) {
            $gids = implode(",", $this->post['gid']);
            $_ENV['views']->update_available($gids,$this->get[2]);
            $message = $this->get[2] ? '景点设为可用成功!':'景点设置过期成功!';
            unset($this->get);
            $this->ondefault($message);

        }
    }

    function onlog($msg='') {
        @$page = max(1, intval($this->get[2]));
        $pagesize=$this->setting['list_default'];
        $startindex = ($page - 1) * $pagesize;
        $loglist = $_ENV['views']->getlog($startindex,$pagesize);

        $viewslognum=$this->db->fetch_total('viewslog');
        $departstr=page($viewsnum, $pagesize, $page,"admin_views/log");
        $msg && $message=$msg;
        $views_range = unserialize($this->setting['views_range']);
        include template("viewsloglist",'admin');
    }
     function onsend() {
        if(isset($this->post['id'])) {
            $ids = implode(",", $this->post['id']);
            $_ENV['views']->update_views_status($ids,$this->get[2]);
            $message = '景点成功设置为已评论！';
            unset($this->get);
            $this->onlog($message);
        }
    }

    

    function onlogsearch(){

        $pricerange = isset($this->get[2])?$this->get[2]:$this->post['pricerange'];
        $viewsname = isset($this->get[3])?$this->get[3]:$this->post['viewsname'];
        $username = isset($this->get[4])?$this->get[4]:$this->post['username'];
        $datestart = isset($this->get[5])?$this->get[5]:$this->post['srchregdatestart'];
        $dateend = isset($this->get[6])?$this->get[6]:$this->post['srchregdateend'];
        @$page = max(1, intval($this->get[7]));
        $pagesize=$this->setting['list_default'];
        $startindex = ($page - 1) * $pagesize;
        $loglist = $_ENV['views']->list_by_searchlog($pricerange,$viewsname,$username,$datestart,$dateend,$startindex,$pagesize);
        $viewslognum = $_ENV['views']->rownum_by_searchlog($pricerange,$viewsname,$username,$datestart,$dateend);
        $departstr=page($viewslognum, $pagesize, $page,"admin_views/logsearch/$pricerange/$viewsname/$username/$datestart/$dateend");
        $views_range = unserialize($this->setting['views_range']);
        include template("viewsloglist",'admin');
    }

}

?>